package com.resurvolution.kingcloudmusicspringboot.common.handler;

import com.resurvolution.kingcloudmusicspringboot.common.constant.ResultConstant;
import com.resurvolution.kingcloudmusicspringboot.common.exception.BaseException;
import com.resurvolution.kingcloudmusicspringboot.common.utils.UnifyResult;
import io.jsonwebtoken.JwtException;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BaseException.class)
    public UnifyResult<Object> handleBaseException(BaseException e, HttpServletResponse response) {
        return UnifyResult.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(JwtException.class)
    public UnifyResult<Object> handleJwtException(JwtException e, HttpServletResponse response) {
        return UnifyResult.error(ResultConstant.UNAUTHORIZED, e.getMessage());
    }

}
